package com.maxlen.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxlen.common.constant.ProductConstant;
import com.maxlen.common.utils.CopyUtils;
import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.Query;
import com.maxlen.goods.dao.AttrAttrgroupRelationDao;
import com.maxlen.goods.dao.AttrDao;
import com.maxlen.goods.dao.AttrGroupDao;
import com.maxlen.goods.entity.AttrAttrgroupRelationEntity;
import com.maxlen.goods.entity.AttrEntity;
import com.maxlen.goods.entity.AttrGroupEntity;
import com.maxlen.goods.entity.dto.AttrAttrgroupRelationInfoDTO;
import com.maxlen.goods.entity.dto.AttrNoInfoDTO;
import com.maxlen.goods.entity.vo.AttrAttrgroupRelationDelVO;
import com.maxlen.goods.service.AttrAttrgroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrAttrgroupRelationService")
public class AttrAttrgroupRelationServiceImpl extends ServiceImpl<AttrAttrgroupRelationDao, AttrAttrgroupRelationEntity> implements AttrAttrgroupRelationService {

    @Autowired
    private AttrDao attrDao;

    @Autowired
    private AttrGroupDao attrGroupDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrAttrgroupRelationEntity> page = this.page(
                new Query<AttrAttrgroupRelationEntity>().getPage(params),
                new QueryWrapper<AttrAttrgroupRelationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 修改关联表中某个属性的分组 id
     * @param attrId
     * @param attrGroupId
     */
    @Override
    public void updateAttrgroup(Long attrId, Long attrGroupId) {

        AttrAttrgroupRelationEntity relationEntity = new AttrAttrgroupRelationEntity();
        relationEntity.setAttrGroupId(attrGroupId);

        Long count = baseMapper.selectCount(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
        if(count.equals(0L)){
            relationEntity.setAttrId(attrId);
            baseMapper.insert(relationEntity);
        }else{
            baseMapper.update(relationEntity, new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrId, attrId));
        }

    }

    /**
     * 根据分组 id 查找关联的所有基本属性
     * @param attrgroupId
     * @return
     */
    @Override
    public List<AttrEntity> getDetailById(Long attrgroupId) {
        List<AttrAttrgroupRelationEntity> entityList = baseMapper.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().eq(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupId));

        if(entityList.isEmpty()){
            return new ArrayList<>();
        }

        List<Long> collect = entityList.stream().map(relation -> {
            return relation.getAttrId();
        }).collect(Collectors.toList());

        List<AttrEntity> attrEntities = attrDao.selectBatchIds(collect);

        return attrEntities;
    }

    /**
     * 根据属性 id 删除关联表的数据
     * @param relationDelVOList
     */
    @Override
    public void removeDetail(List<AttrAttrgroupRelationDelVO> relationDelVOList) {
        if(relationDelVOList.isEmpty()){
            return;
        }
        List<Long> attrIdList = relationDelVOList.stream().map(item -> {
            return item.getAttrId();
        }).collect(Collectors.toList());
        baseMapper.delete(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(AttrAttrgroupRelationEntity::getAttrId, attrIdList));

    }

    /**
     * 获取没有分组的属性 分页
     * @param params
     * @return
     */
    @Override
    public PageUtils getNoDetailById(Map<String, Object> params, Long attrgroupId) {

        // 查找相同分类的属性
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrgroupId);
        List<AttrGroupEntity> attrGroupEntityList = attrGroupDao.selectList(new LambdaQueryWrapper<AttrGroupEntity>().eq(AttrGroupEntity::getCatelogId, attrGroupEntity.getCatelogId()));
        List<Long> attrgroupIdList = attrGroupEntityList.stream().map(item -> {
            return item.getAttrGroupId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<AttrEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AttrEntity::getCatelogId, attrGroupEntity.getCatelogId());
        wrapper.eq(AttrEntity::getAttrType, ProductConstant.AttrEnum.ATTR_TYPE_BASE.getCode());

        if(!attrgroupIdList.isEmpty()){
            List<AttrAttrgroupRelationEntity> relationEntityList = baseMapper.selectList(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(AttrAttrgroupRelationEntity::getAttrGroupId, attrgroupIdList));
            List<Long> attrIdList = relationEntityList.stream().map(item -> {
                return item.getAttrId();
            }).collect(Collectors.toList());

            if(!attrIdList.isEmpty()){
                wrapper.notIn(AttrEntity::getAttrId, attrIdList);
            }
        }

        IPage<AttrEntity> page = attrDao.selectPage(new Query<AttrEntity>().getPage(params), wrapper);
        PageUtils pageUtils = new PageUtils(page);

        return pageUtils;
    }

    /**
     * 根据属性 id 和分组 id 保存到数据库中
     * @param voList
     */
    @Override
    @Transactional
    public void saveDetail(List<AttrAttrgroupRelationDelVO> voList) {
        if(voList.isEmpty()){
            return;
        }
        List<AttrAttrgroupRelationEntity> relationEntityList = CopyUtils.copyList(voList, AttrAttrgroupRelationEntity.class);
        this.saveBatch(relationEntityList);
    }

    /**
     * 根据属性 id 真正地删除数据
     * @param attrId
     */
    @Override
    public void removeDetailByAttrId(List<Long> attrId) {
        baseMapper.delete(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(AttrAttrgroupRelationEntity::getAttrId, attrId));

    }

    @Override
    public void removeDetailByAttrgroupId(List<Long> attrGroupIds) {
        baseMapper.delete(new LambdaQueryWrapper<AttrAttrgroupRelationEntity>().in(AttrAttrgroupRelationEntity::getAttrGroupId, attrGroupIds));
    }

}
